package com.gzgy.ustore.api.web.controller.webservice;


import com.gzgy.ustore.api.common.exception.BaseAPIException;
import com.gzgy.ustore.api.common.exception.ExceptionCodeStatusEnum;
import com.gzgy.ustore.api.common.util.I18nUtil;
import com.gzgy.ustore.api.service.UserService;
import com.gzgy.ustore.api.util.ResultUtil;
import com.gzgy.ustore.api.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.sql.SQLException;

@Controller
public class UserController extends BaseController{

    @Autowired
    private UserService userService;

    @RequestMapping(value = "loginWebPage",method = RequestMethod.POST)
    private String loginWebPage(ModelMap modelMap,String userCode,String userPwd,String companyId){

        Assert.hasText(userCode,"userCode " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(userPwd,"userPwd " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(companyId,"companyId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(companyId,"companyId " + I18nUtil.getMessage(-2,null,"zh_CN"));

        modelMap.put("message", ResultUtil.getResultJson(userService.loginWebPage(userCode,userPwd,companyId)));

        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "userList",method = RequestMethod.POST)
    private String userList(ModelMap modelMap,String userId,String session,String companyId,String index,String page){

        Assert.hasText(companyId,"companyId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(index,"index " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(page,"page " + I18nUtil.getMessage(-2,null,"zh_CN"));

        modelMap.put("message", ResultUtil.getResultJson(userService.getUserList(companyId,index,page)));

        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "addUser",method = RequestMethod.POST)
    private String addUser(ModelMap modelMap,String userId,String session,String userCode,String userName,String userPwd,
                           String orgId,String roleId,String companyId,String email,String phone ){

        Assert.hasText(companyId,"companyId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(userCode,"userCode " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(userName,"userName " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(userPwd,"userPwd " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(orgId,"orgId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(roleId,"roleId " + I18nUtil.getMessage(-2,null,"zh_CN"));

        modelMap.put("message", ResultUtil.getResultJson(userService.addUser(userCode,userName,userPwd,companyId,orgId,roleId,email,phone,userId)));

        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "userOperation",method = RequestMethod.POST)
    private String userOperation(ModelMap modelMap,String userId,String session,String userName,
                           String orgId,String roleId,String email,String phone ,String flag,String uId) throws SQLException {


        Assert.hasText(flag,"flag " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(uId,"uId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        if("0".equals(flag)){
            Assert.hasText(userName,"userName " + I18nUtil.getMessage(-2,null,"zh_CN"));
            Assert.hasText(orgId,"orgId " + I18nUtil.getMessage(-2,null,"zh_CN"));
            Assert.hasText(roleId,"roleId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        }else if(!"1".equals(flag)){
            int errorCode = ExceptionCodeStatusEnum.UNKOWN_PARAMTER_EXCEPTION.toInt();
            String errorMsg = I18nUtil.getMessage(errorCode, null, "zh_CN");
            throw new BaseAPIException(errorMsg, errorCode);
        }
        modelMap.put("message", ResultUtil.getResultJson(userService.userOperation(flag,uId,userId,userName,orgId,roleId,email,phone)));

        return SHOW_JSON_NAME;
    }
}
